package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.drink.DrinkBlock;
import com.yanqu.road.entity.activity.drink.DrinkEliminateBoom;
import com.yanqu.road.entity.activity.drink.DrinkEliminateRoundResult;
import com.yanqu.road.entity.activity.drink.DrinkEliminateStageResult;
import com.yanqu.road.entity.activity.drink.DrinkOrder;
import com.yanqu.road.entity.activity.drink.DrinkOrderProg;
import com.yanqu.road.entity.activity.drink.DrinkUserData;
import com.yanqu.road.entity.activity.drink.config.DrinkBlockConfig;
import com.yanqu.road.entity.activity.drink.config.DrinkConfig;
import com.yanqu.road.entity.activity.drink.config.DrinkOrderConfig;
import com.yanqu.road.entity.activity.drink.config.DrinkScoreConfig;
import com.yanqu.road.pb.activity.DrinkActivityProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

public class DrinkPb {

    /**
     * 配置
     */
    public static DrinkActivityProto.DrinkSyncConfigRespMsg.Builder parseDrinkSyncConfigRespMsg(DrinkConfig config, String language) {
        DrinkActivityProto.DrinkSyncConfigRespMsg.Builder builder = DrinkActivityProto.DrinkSyncConfigRespMsg.newBuilder();
        for (DrinkBlockConfig blockConfig : config.getBlockConfigMap().values()) {
            builder.addBlockConfigList(parseDrinkBlockConfigMsg(blockConfig, language));
        }
        for (DrinkOrderConfig orderConfig : config.getOrderConfigMap().values()) {
            builder.addOrderConfigList(parseDrinkOrderConfigMsg(orderConfig, language));
        }
        for (DrinkScoreConfig scoreConfig : config.getScoreConfigMap().values()) {
            builder.addScoreConfigList(parseDrinkScoreConfigMsg(scoreConfig));
        }
        return builder;
    }

    public static DrinkActivityProto.DrinkBlockConfigMsg.Builder parseDrinkBlockConfigMsg(DrinkBlockConfig config, String language) {
        DrinkActivityProto.DrinkBlockConfigMsg.Builder builder = DrinkActivityProto.DrinkBlockConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setType(config.getType());
        return builder;
    }

    public static DrinkActivityProto.DrinkOrderConfigMsg.Builder parseDrinkOrderConfigMsg(DrinkOrderConfig config, String language) {
        DrinkActivityProto.DrinkOrderConfigMsg.Builder builder = DrinkActivityProto.DrinkOrderConfigMsg.newBuilder();
        builder.setOrderId(config.getOrderId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setSpecParam(config.getSpecParam());
        builder.setNpcId(config.getNpcId());
        builder.setNpcName(ServerLanguageMgr.getContent(config.getNpcName(), language));
        builder.setNpcDesc(ServerLanguageMgr.getContent(config.getNpcDesc(), language));
        return builder;
    }

    public static DrinkActivityProto.DrinkScoreConfigMsg.Builder parseDrinkScoreConfigMsg(DrinkScoreConfig config) {
        DrinkActivityProto.DrinkScoreConfigMsg.Builder builder = DrinkActivityProto.DrinkScoreConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setContinuousNum(config.getContinuousNum());
        builder.setPic(config.getPic());
        builder.setScoreAdd(config.getScoreAdd());
        return builder;
    }

    /**
     * 游戏数据
     */
    public static DrinkActivityProto.DrinkGameDataMsg.Builder parseDrinkGameDataMsg(DrinkUserData drinkUserData, int multi) {
        DrinkActivityProto.DrinkGameDataMsg.Builder builder = DrinkActivityProto.DrinkGameDataMsg.newBuilder();
        int[][] blockList = drinkUserData.getBlockList(multi);
        for (int y = 0; y < blockList.length; y++) {
            for (int x = 0; x < blockList[y].length; x++) {
                builder.addBlockMsgList(parseDrinkBlockMsg(x, y, blockList[y][x]));
            }
        }
        builder.setCurOrder(parseDrinkOrderMsg(drinkUserData.getOrder(multi)));
        builder.setFinishOrderCount(
                drinkUserData.getWaitRewardOrderList(1).size()
                        + drinkUserData.getWaitRewardOrderList(8).size()
                        + drinkUserData.getWaitRewardOrderList(16).size());
        builder.setEnergy(drinkUserData.getEnergy());
        builder.setLastRecTime(drinkUserData.getLastRecTime());
        builder.setScore(drinkUserData.getScore());
        builder.addOrderIdList(drinkUserData.getOrder(multi).getoId()); // 订单ID列表 = 当前订单ID + 后续订单ID
        builder.addAllOrderIdList(drinkUserData.getOrderIdList(multi));
        builder.setMulti(multi != 1);
        builder.setNewMulti(multi);
        return builder;
    }

    /**
     * 方块（不带积分）
     */
    public static DrinkActivityProto.DrinkBlockMsg.Builder parseDrinkBlockMsg(int x, int y, int c) {
        DrinkActivityProto.DrinkBlockMsg.Builder builder = DrinkActivityProto.DrinkBlockMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setColor(c);
        builder.setScore(0);
        return builder;
    }

    /**
     * 方块
     */
    public static DrinkActivityProto.DrinkBlockMsg.Builder parseDrinkBlockMsg(DrinkBlock drinkBlock) {
        DrinkActivityProto.DrinkBlockMsg.Builder builder = DrinkActivityProto.DrinkBlockMsg.newBuilder();
        builder.setX(drinkBlock.getX());
        builder.setY(drinkBlock.getY());
        builder.setColor(drinkBlock.getC());
        builder.setScore(drinkBlock.getScore());
        return builder;
    }

    /**
     * 订单
     */
    public static DrinkActivityProto.DrinkOrderMsg.Builder parseDrinkOrderMsg(DrinkOrder order) {
        DrinkActivityProto.DrinkOrderMsg.Builder builder = DrinkActivityProto.DrinkOrderMsg.newBuilder();
        builder.setOrderId(order.getoId());
        for (DrinkOrderProg orderProg : order.getProgMap().values()) {
            builder.addProgList(parseDrinkOrderProgMsg(orderProg));
        }
        builder.setStep(order.getStep());
        builder.setMaxStep(order.getMaxStep());
        builder.setReward(order.getReward());
        return builder;
    }

    /**
     * 订单进度
     */
    public static DrinkActivityProto.DrinkOrderProgMsg.Builder parseDrinkOrderProgMsg(DrinkOrderProg orderProg) {
        DrinkActivityProto.DrinkOrderProgMsg.Builder builder = DrinkActivityProto.DrinkOrderProgMsg.newBuilder();
        builder.setBlockId(orderProg.getbId());
        builder.setCur(orderProg.getCur());
        builder.setNeed(orderProg.getNeed());
        return builder;
    }

    /**
     * 每阶段奖励
     */
    public static DrinkActivityProto.DrinkEliminateStageMsg.Builder parseDrinkEliminateStageMsg(DrinkEliminateStageResult stageResult) {
        DrinkActivityProto.DrinkEliminateStageMsg.Builder builder = DrinkActivityProto.DrinkEliminateStageMsg.newBuilder();
        for (DrinkEliminateBoom eliminateBoom : stageResult.getBoomList()) {
            builder.addBoomList(parseDrinkBoomMsg(eliminateBoom));
        }
        for (DrinkBlock drinkBlock : stageResult.getBlockList()) {
            builder.addBlockList(parseDrinkBlockMsg(drinkBlock));
        }
        return builder;
    }

    /**
     * 爆炸信息
     */
    public static DrinkActivityProto.DrinkBoomMsg.Builder parseDrinkBoomMsg(DrinkEliminateBoom boom) {
        DrinkActivityProto.DrinkBoomMsg.Builder builder = DrinkActivityProto.DrinkBoomMsg.newBuilder();
        builder.setX(boom.getX());
        builder.setY(boom.getY());
        builder.setType(boom.getType());
        return builder;
    }

    /**
     * 每轮数据
     */
    public static DrinkActivityProto.DrinkEliminateRoundMsg.Builder parseDrinkEliminateRoundMsg(DrinkEliminateRoundResult result) {
        DrinkActivityProto.DrinkEliminateRoundMsg.Builder builder = DrinkActivityProto.DrinkEliminateRoundMsg.newBuilder();
        builder.setType(result.getType());
        for (DrinkEliminateStageResult stageResult : result.getStageResultList()) {
            builder.addStageList(parseDrinkEliminateStageMsg(stageResult));
        }
        for (DrinkBlock block : result.getCreateBomb()) {
            builder.addCreateBombList(parseDrinkBlockMsg(block));
        }
        if (result.getOrderBomb() != null) {
            builder.setOrderGiveBombList(parseDrinkBlockMsg(result.getOrderBomb()));
        }
        for (int y = 0; y < result.getBlockList().length; y++) {
            for (int x = 0; x < result.getBlockList()[y].length; x++) {
                builder.addBlockMsgList(parseDrinkBlockMsg(x, y, result.getBlockList()[y][x]));
            }
        }
        builder.setCurOrder(parseDrinkOrderMsg(result.getCurOrder()));
        if (result.getFinishOrder() != null) {
            builder.setFinishOrder(parseDrinkOrderMsg(result.getFinishOrder()));
        }
        if (result.getFailOrder() != null) {
            builder.setFailOrder(parseDrinkOrderMsg(result.getFailOrder()));
        }
        if (result.getAddOrderId() != -1) {
            builder.setAddOrderId(result.getAddOrderId());
        }
        return builder;
    }

}
